
<!DOCTYPE html>

<html>
  
<!-- Mirrored from docs.sympy.org/latest/tutorial/calculus.html by HTTrack Website Copier/3.x [XR&CO'2014], Sat, 15 Jan 2022 03:23:56 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=utf-8" /><!-- /Added by HTTrack -->
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />

    <title>Calculus &#8212; SymPy 1.9 documentation</title>
    <link rel="stylesheet" type="text/css" href="../_static/pygments.css" />
    <link rel="stylesheet" type="text/css" href="../_static/default.css" />
    <link rel="stylesheet" type="text/css" href="../_static/graphviz.css" />
    <link rel="stylesheet" type="text/css" href="../_static/plot_directive.css" />
    <link rel="stylesheet" type="text/css" href="../../../live.sympy.org/static/live-core.css" />
    <link rel="stylesheet" type="text/css" href="../../../live.sympy.org/static/live-autocomplete.css" />
    <link rel="stylesheet" type="text/css" href="../../../live.sympy.org/static/live-sphinx.css" />
    
    <script data-url_root="../" id="documentation_options" src="../_static/documentation_options.js"></script>
    <script src="../_static/jquery.js"></script>
    <script src="../_static/underscore.js"></script>
    <script src="../_static/doctools.js"></script>
    <script src="../../../live.sympy.org/static/utilities.js"></script>
    <script src="../../../live.sympy.org/static/external/classy.js"></script>
    <script src="../../../live.sympy.org/static/live-core.js"></script>
    <script src="../../../live.sympy.org/static/live-autocomplete.js"></script>
    <script src="../../../live.sympy.org/static/live-sphinx.js"></script>
    <script async="async" src="../../../cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest8331.js?config=TeX-AMS_HTML-full"></script>
    <script type="text/x-mathjax-config">MathJax.Hub.Config({"tex2jax": {"inlineMath": [["\\(", "\\)"]], "displayMath": [["\\[", "\\]"]]}})</script>
    
    <link rel="shortcut icon" href="../_static/sympy-notailtext-favicon.ico"/>
    <link href="calculus.html" rel="canonical" />
    
    <link rel="index" title="Index" href="../genindex.html" />
    <link rel="search" title="Search" href="../search.html" />
    <link rel="next" title="Solvers" href="solvers.html" />
    <link rel="prev" title="Simplification" href="simplification.html" /> 
  </head><body>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="solvers.html" title="Solvers"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="simplification.html" title="Simplification"
             accesskey="P">previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="../index.html">SymPy 1.9 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="index.html" accesskey="U">SymPy Tutorial</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="#">Calculus</a></li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <section id="calculus">
<span id="id1"></span><h1>Calculus<a class="headerlink" href="#calculus" title="Permalink to this headline">¶</a></h1>
<p>This section covers how to do basic calculus tasks such as derivatives,
integrals, limits, and series expansions in SymPy.  If you are not familiar
with the math of any part of this section, you may safely skip it.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="o">*</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span> <span class="o">=</span> <span class="n">symbols</span><span class="p">(</span><span class="s1">&#39;x y z&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">init_printing</span><span class="p">(</span><span class="n">use_unicode</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
</pre></div>
</div>
<section id="derivatives">
<span id="tutorial-derivatives"></span><h2>Derivatives<a class="headerlink" href="#derivatives" title="Permalink to this headline">¶</a></h2>
<p>To take derivatives, use the <code class="docutils literal notranslate"><span class="pre">diff</span></code> function.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">diff</span><span class="p">(</span><span class="n">cos</span><span class="p">(</span><span class="n">x</span><span class="p">),</span> <span class="n">x</span><span class="p">)</span>
<span class="go">-sin(x)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">diff</span><span class="p">(</span><span class="n">exp</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span><span class="p">),</span> <span class="n">x</span><span class="p">)</span>
<span class="go">     ⎛ 2⎞</span>
<span class="go">     ⎝x ⎠</span>
<span class="go">2⋅x⋅ℯ</span>
</pre></div>
</div>
<p><code class="docutils literal notranslate"><span class="pre">diff</span></code> can take multiple derivatives at once.  To take multiple derivatives,
pass the variable as many times as you wish to differentiate, or pass a number
after the variable.  For example, both of the following find the third
derivative of <span class="math notranslate nohighlight">\(x^4\)</span>.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">diff</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="mi">4</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">x</span><span class="p">)</span>
<span class="go">24⋅x</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">diff</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="mi">4</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>
<span class="go">24⋅x</span>
</pre></div>
</div>
<p>You can also take derivatives with respect to many variables at once.  Just
pass each derivative in order, using the same syntax as for single variable
derivatives.  For example, each of the following will compute
<span class="math notranslate nohighlight">\(\frac{\partial^7}{\partial x\partial y^2\partial z^4} e^{x y z}\)</span>.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span> <span class="o">=</span> <span class="n">exp</span><span class="p">(</span><span class="n">x</span><span class="o">*</span><span class="n">y</span><span class="o">*</span><span class="n">z</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">diff</span><span class="p">(</span><span class="n">expr</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span><span class="p">,</span> <span class="n">z</span><span class="p">,</span> <span class="n">z</span><span class="p">,</span> <span class="n">z</span><span class="p">)</span>
<span class="go"> 3  2 ⎛ 3  3  3       2  2  2                ⎞  x⋅y⋅z</span>
<span class="go">x ⋅y ⋅⎝x ⋅y ⋅z  + 14⋅x ⋅y ⋅z  + 52⋅x⋅y⋅z + 48⎠⋅ℯ</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">diff</span><span class="p">(</span><span class="n">expr</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="n">z</span><span class="p">,</span> <span class="mi">4</span><span class="p">)</span>
<span class="go"> 3  2 ⎛ 3  3  3       2  2  2                ⎞  x⋅y⋅z</span>
<span class="go">x ⋅y ⋅⎝x ⋅y ⋅z  + 14⋅x ⋅y ⋅z  + 52⋅x⋅y⋅z + 48⎠⋅ℯ</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">diff</span><span class="p">(</span><span class="n">expr</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span><span class="p">,</span> <span class="mi">4</span><span class="p">)</span>
<span class="go"> 3  2 ⎛ 3  3  3       2  2  2                ⎞  x⋅y⋅z</span>
<span class="go">x ⋅y ⋅⎝x ⋅y ⋅z  + 14⋅x ⋅y ⋅z  + 52⋅x⋅y⋅z + 48⎠⋅ℯ</span>
</pre></div>
</div>
<p><code class="docutils literal notranslate"><span class="pre">diff</span></code> can also be called as a method.  The two ways of calling <code class="docutils literal notranslate"><span class="pre">diff</span></code> are
exactly the same, and are provided only for convenience.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span><span class="o">.</span><span class="n">diff</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span><span class="p">,</span> <span class="mi">4</span><span class="p">)</span>
<span class="go"> 3  2 ⎛ 3  3  3       2  2  2                ⎞  x⋅y⋅z</span>
<span class="go">x ⋅y ⋅⎝x ⋅y ⋅z  + 14⋅x ⋅y ⋅z  + 52⋅x⋅y⋅z + 48⎠⋅ℯ</span>
</pre></div>
</div>
<p>To create an unevaluated derivative, use the <code class="docutils literal notranslate"><span class="pre">Derivative</span></code> class.  It has the
same syntax as <code class="docutils literal notranslate"><span class="pre">diff</span></code>.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">deriv</span> <span class="o">=</span> <span class="n">Derivative</span><span class="p">(</span><span class="n">expr</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span><span class="p">,</span> <span class="mi">4</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">deriv</span>
<span class="go">     7</span>
<span class="go">    ∂     ⎛ x⋅y⋅z⎞</span>
<span class="go">──────────⎝ℯ     ⎠</span>
<span class="go">  4   2</span>
<span class="go">∂z  ∂y  ∂x</span>
</pre></div>
</div>
<p>To evaluate an unevaluated derivative, use the <code class="docutils literal notranslate"><span class="pre">doit</span></code> method.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">deriv</span><span class="o">.</span><span class="n">doit</span><span class="p">()</span>
<span class="go"> 3  2 ⎛ 3  3  3       2  2  2                ⎞  x⋅y⋅z</span>
<span class="go">x ⋅y ⋅⎝x ⋅y ⋅z  + 14⋅x ⋅y ⋅z  + 52⋅x⋅y⋅z + 48⎠⋅ℯ</span>
</pre></div>
</div>
<p>These unevaluated objects are useful for delaying the evaluation of the
derivative, or for printing purposes.  They are also used when SymPy does not
know how to compute the derivative of an expression (for example, if it
contains an undefined function, which are described in the <a class="reference internal" href="solvers.html#tutorial-dsolve"><span class="std std-ref">Solving
Differential Equations</span></a> section).</p>
<p>Derivatives of unspecified order can be created using tuple <code class="docutils literal notranslate"><span class="pre">(x,</span> <span class="pre">n)</span></code> where
<code class="docutils literal notranslate"><span class="pre">n</span></code> is the order of the derivative with respect to <code class="docutils literal notranslate"><span class="pre">x</span></code>.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">m</span><span class="p">,</span> <span class="n">n</span><span class="p">,</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span> <span class="o">=</span> <span class="n">symbols</span><span class="p">(</span><span class="s1">&#39;m n a b&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span> <span class="o">=</span> <span class="p">(</span><span class="n">a</span><span class="o">*</span><span class="n">x</span> <span class="o">+</span> <span class="n">b</span><span class="p">)</span><span class="o">**</span><span class="n">m</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span><span class="o">.</span><span class="n">diff</span><span class="p">((</span><span class="n">x</span><span class="p">,</span> <span class="n">n</span><span class="p">))</span>
<span class="go">  n</span>
<span class="go"> ∂ ⎛         m⎞</span>
<span class="go">───⎝(a⋅x + b) ⎠</span>
<span class="go">  n</span>
<span class="go">∂x</span>
</pre></div>
</div>
</section>
<section id="integrals">
<h2>Integrals<a class="headerlink" href="#integrals" title="Permalink to this headline">¶</a></h2>
<p>To compute an integral, use the <code class="docutils literal notranslate"><span class="pre">integrate</span></code> function.  There are two kinds
of integrals, definite and indefinite.  To compute an indefinite integral,
that is, an antiderivative, or primitive, just pass the variable after the
expression.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">integrate</span><span class="p">(</span><span class="n">cos</span><span class="p">(</span><span class="n">x</span><span class="p">),</span> <span class="n">x</span><span class="p">)</span>
<span class="go">sin(x)</span>
</pre></div>
</div>
<p>Note that SymPy does not include the constant of integration.  If you want it,
you can add one yourself, or rephrase your problem as a differential equation
and use <code class="docutils literal notranslate"><span class="pre">dsolve</span></code> to solve it, which does add the constant (see <a class="reference internal" href="solvers.html#tutorial-dsolve"><span class="std std-ref">Solving Differential Equations</span></a>).</p>
<aside class="sidebar">
<p class="sidebar-title">Quick Tip</p>
<p><span class="math notranslate nohighlight">\(\infty\)</span> in SymPy is <code class="docutils literal notranslate"><span class="pre">oo</span></code> (that’s the lowercase letter “oh” twice).  This
is because <code class="docutils literal notranslate"><span class="pre">oo</span></code> looks like <span class="math notranslate nohighlight">\(\infty\)</span>, and is easy to type.</p>
</aside>
<p>To compute a definite integral, pass the argument <code class="docutils literal notranslate"><span class="pre">(integration_variable,</span>
<span class="pre">lower_limit,</span> <span class="pre">upper_limit)</span></code>.  For example, to compute</p>
<div class="math notranslate nohighlight">
\[\int_0^\infty e^{-x}\,dx,\]</div>
<p>we would do</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">integrate</span><span class="p">(</span><span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="n">x</span><span class="p">),</span> <span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">oo</span><span class="p">))</span>
<span class="go">1</span>
</pre></div>
</div>
<p>As with indefinite integrals, you can pass multiple limit tuples to perform a
multiple integral.  For example, to compute</p>
<div class="math notranslate nohighlight">
\[\int_{-\infty}^{\infty}\int_{-\infty}^{\infty} e^{- x^{2} - y^{2}}\, dx\, dy,\]</div>
<p>do</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">integrate</span><span class="p">(</span><span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="o">-</span> <span class="n">y</span><span class="o">**</span><span class="mi">2</span><span class="p">),</span> <span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="o">-</span><span class="n">oo</span><span class="p">,</span> <span class="n">oo</span><span class="p">),</span> <span class="p">(</span><span class="n">y</span><span class="p">,</span> <span class="o">-</span><span class="n">oo</span><span class="p">,</span> <span class="n">oo</span><span class="p">))</span>
<span class="go">π</span>
</pre></div>
</div>
<p>If <code class="docutils literal notranslate"><span class="pre">integrate</span></code> is unable to compute an integral, it returns an unevaluated
<code class="docutils literal notranslate"><span class="pre">Integral</span></code> object.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span> <span class="o">=</span> <span class="n">integrate</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="n">x</span><span class="p">,</span> <span class="n">x</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">expr</span><span class="p">)</span>
<span class="go">Integral(x**x, x)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span>
<span class="go">⌠</span>
<span class="go">⎮  x</span>
<span class="go">⎮ x  dx</span>
<span class="go">⌡</span>
</pre></div>
</div>
<p>As with <code class="docutils literal notranslate"><span class="pre">Derivative</span></code>, you can create an unevaluated integral using
<code class="docutils literal notranslate"><span class="pre">Integral</span></code>.  To later evaluate this integral, call <code class="docutils literal notranslate"><span class="pre">doit</span></code>.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span> <span class="o">=</span> <span class="n">Integral</span><span class="p">(</span><span class="n">log</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">**</span><span class="mi">2</span><span class="p">,</span> <span class="n">x</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span>
<span class="go">⌠</span>
<span class="go">⎮    2</span>
<span class="go">⎮ log (x) dx</span>
<span class="go">⌡</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span><span class="o">.</span><span class="n">doit</span><span class="p">()</span>
<span class="go">         2</span>
<span class="go">x⋅log (x) - 2⋅x⋅log(x) + 2⋅x</span>
</pre></div>
</div>
<p><code class="docutils literal notranslate"><span class="pre">integrate</span></code> uses powerful algorithms that are always improving to compute
both definite and indefinite integrals, including heuristic pattern matching
type algorithms, a partial implementation of the <a class="reference external" href="https://en.wikipedia.org/wiki/Risch_algorithm">Risch algorithm</a>, and an algorithm using
<a class="reference external" href="https://en.wikipedia.org/wiki/Meijer_g-function">Meijer G-functions</a> that is
useful for computing integrals in terms of special functions, especially
definite integrals.  Here is a sampling of some of the power of <code class="docutils literal notranslate"><span class="pre">integrate</span></code>.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">integ</span> <span class="o">=</span> <span class="n">Integral</span><span class="p">((</span><span class="n">x</span><span class="o">**</span><span class="mi">4</span> <span class="o">+</span> <span class="n">x</span><span class="o">**</span><span class="mi">2</span><span class="o">*</span><span class="n">exp</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="o">-</span> <span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="o">-</span> <span class="mi">2</span><span class="o">*</span><span class="n">x</span><span class="o">*</span><span class="n">exp</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="o">-</span> <span class="mi">2</span><span class="o">*</span><span class="n">x</span> <span class="o">-</span>
<span class="gp">... </span>    <span class="n">exp</span><span class="p">(</span><span class="n">x</span><span class="p">))</span><span class="o">*</span><span class="n">exp</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">/</span><span class="p">((</span><span class="n">x</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span><span class="o">**</span><span class="mi">2</span><span class="o">*</span><span class="p">(</span><span class="n">x</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span><span class="o">**</span><span class="mi">2</span><span class="o">*</span><span class="p">(</span><span class="n">exp</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)),</span> <span class="n">x</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">integ</span>
<span class="go">⌠</span>
<span class="go">⎮ ⎛ 4    2  x    2        x          x⎞  x</span>
<span class="go">⎮ ⎝x  + x ⋅ℯ  - x  - 2⋅x⋅ℯ  - 2⋅x - ℯ ⎠⋅ℯ</span>
<span class="go">⎮ ──────────────────────────────────────── dx</span>
<span class="go">⎮               2        2 ⎛ x    ⎞</span>
<span class="go">⎮        (x - 1) ⋅(x + 1) ⋅⎝ℯ  + 1⎠</span>
<span class="go">⌡</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">integ</span><span class="o">.</span><span class="n">doit</span><span class="p">()</span>
<span class="go">                 x</span>
<span class="go">   ⎛ x    ⎞     ℯ</span>
<span class="go">log⎝ℯ  + 1⎠ + ──────</span>
<span class="go">               2</span>
<span class="go">              x  - 1</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">integ</span> <span class="o">=</span> <span class="n">Integral</span><span class="p">(</span><span class="n">sin</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span><span class="p">),</span> <span class="n">x</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">integ</span>
<span class="go">⌠</span>
<span class="go">⎮    ⎛ 2⎞</span>
<span class="go">⎮ sin⎝x ⎠ dx</span>
<span class="go">⌡</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">integ</span><span class="o">.</span><span class="n">doit</span><span class="p">()</span>
<span class="go">         ⎛√2⋅x⎞</span>
<span class="go">3⋅√2⋅√π⋅S⎜────⎟⋅Γ(3/4)</span>
<span class="go">         ⎝ √π ⎠</span>
<span class="go">──────────────────────</span>
<span class="go">       8⋅Γ(7/4)</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">integ</span> <span class="o">=</span> <span class="n">Integral</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="n">y</span><span class="o">*</span><span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="n">x</span><span class="p">),</span> <span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">oo</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">integ</span>
<span class="go">∞</span>
<span class="go">⌠</span>
<span class="go">⎮  y  -x</span>
<span class="go">⎮ x ⋅ℯ   dx</span>
<span class="go">⌡</span>
<span class="go">0</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">integ</span><span class="o">.</span><span class="n">doit</span><span class="p">()</span>
<span class="go">⎧ Γ(y + 1)    for re(y) &gt; -1</span>
<span class="go">⎪</span>
<span class="go">⎪∞</span>
<span class="go">⎪⌠</span>
<span class="go">⎨⎮  y  -x</span>
<span class="go">⎪⎮ x ⋅ℯ   dx    otherwise</span>
<span class="go">⎪⌡</span>
<span class="go">⎪0</span>
<span class="go">⎩</span>
</pre></div>
</div>
<p>This last example returned a <code class="docutils literal notranslate"><span class="pre">Piecewise</span></code> expression because the integral
does not converge unless <span class="math notranslate nohighlight">\(\Re(y) &gt; 1.\)</span></p>
</section>
<section id="limits">
<h2>Limits<a class="headerlink" href="#limits" title="Permalink to this headline">¶</a></h2>
<p>SymPy can compute symbolic limits with the <code class="docutils literal notranslate"><span class="pre">limit</span></code> function.  The syntax to compute</p>
<div class="math notranslate nohighlight">
\[\lim_{x\to x_0} f(x)\]</div>
<p>is <code class="docutils literal notranslate"><span class="pre">limit(f(x),</span> <span class="pre">x,</span> <span class="pre">x0)</span></code>.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">limit</span><span class="p">(</span><span class="n">sin</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">/</span><span class="n">x</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
<span class="go">1</span>
</pre></div>
</div>
<p><code class="docutils literal notranslate"><span class="pre">limit</span></code> should be used instead of <code class="docutils literal notranslate"><span class="pre">subs</span></code> whenever the point of evaluation
is a singularity.  Even though SymPy has objects to represent <span class="math notranslate nohighlight">\(\infty\)</span>, using
them for evaluation is not reliable because they do not keep track of things
like rate of growth.  Also, things like <span class="math notranslate nohighlight">\(\infty - \infty\)</span> and
<span class="math notranslate nohighlight">\(\frac{\infty}{\infty}\)</span> return <span class="math notranslate nohighlight">\(\mathrm{nan}\)</span> (not-a-number).  For example</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span> <span class="o">=</span> <span class="n">x</span><span class="o">**</span><span class="mi">2</span><span class="o">/</span><span class="n">exp</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span><span class="o">.</span><span class="n">subs</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">oo</span><span class="p">)</span>
<span class="go">nan</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">limit</span><span class="p">(</span><span class="n">expr</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">oo</span><span class="p">)</span>
<span class="go">0</span>
</pre></div>
</div>
<p>Like <code class="docutils literal notranslate"><span class="pre">Derivative</span></code> and <code class="docutils literal notranslate"><span class="pre">Integral</span></code>, <code class="docutils literal notranslate"><span class="pre">limit</span></code> has an unevaluated
counterpart, <code class="docutils literal notranslate"><span class="pre">Limit</span></code>.  To evaluate it, use <code class="docutils literal notranslate"><span class="pre">doit</span></code>.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span> <span class="o">=</span> <span class="n">Limit</span><span class="p">((</span><span class="n">cos</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span><span class="o">/</span><span class="n">x</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span>
<span class="go">     ⎛cos(x) - 1⎞</span>
<span class="go"> lim ⎜──────────⎟</span>
<span class="go">x─→0⁺⎝    x     ⎠</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span><span class="o">.</span><span class="n">doit</span><span class="p">()</span>
<span class="go">0</span>
</pre></div>
</div>
<p>To evaluate a limit at one side only, pass <code class="docutils literal notranslate"><span class="pre">'+'</span></code> or <code class="docutils literal notranslate"><span class="pre">'-'</span></code> as a fourth
argument to <code class="docutils literal notranslate"><span class="pre">limit</span></code>.  For example, to compute</p>
<div class="math notranslate nohighlight">
\[\lim_{x\to 0^+}\frac{1}{x},\]</div>
<p>do</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">limit</span><span class="p">(</span><span class="mi">1</span><span class="o">/</span><span class="n">x</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="s1">&#39;+&#39;</span><span class="p">)</span>
<span class="go">∞</span>
</pre></div>
</div>
<p>As opposed to</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">limit</span><span class="p">(</span><span class="mi">1</span><span class="o">/</span><span class="n">x</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="s1">&#39;-&#39;</span><span class="p">)</span>
<span class="go">-∞</span>
</pre></div>
</div>
</section>
<section id="series-expansion">
<h2>Series Expansion<a class="headerlink" href="#series-expansion" title="Permalink to this headline">¶</a></h2>
<p>SymPy can compute asymptotic series expansions of functions around a point. To
compute the expansion of <span class="math notranslate nohighlight">\(f(x)\)</span> around the point <span class="math notranslate nohighlight">\(x = x_0\)</span> terms of order
<span class="math notranslate nohighlight">\(x^n\)</span>, use <code class="docutils literal notranslate"><span class="pre">f(x).series(x,</span> <span class="pre">x0,</span> <span class="pre">n)</span></code>.  <code class="docutils literal notranslate"><span class="pre">x0</span></code> and <code class="docutils literal notranslate"><span class="pre">n</span></code> can be omitted, in
which case the defaults <code class="docutils literal notranslate"><span class="pre">x0=0</span></code> and <code class="docutils literal notranslate"><span class="pre">n=6</span></code> will be used.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span> <span class="o">=</span> <span class="n">exp</span><span class="p">(</span><span class="n">sin</span><span class="p">(</span><span class="n">x</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span><span class="o">.</span><span class="n">series</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">4</span><span class="p">)</span>
<span class="go">         2</span>
<span class="go">        x     ⎛ 4⎞</span>
<span class="go">1 + x + ── + O⎝x ⎠</span>
<span class="go">        2</span>
</pre></div>
</div>
<p>The <span class="math notranslate nohighlight">\(O\left(x^4\right)\)</span> term at the end represents the Landau order term at
<span class="math notranslate nohighlight">\(x=0\)</span> (not to be confused with big O notation used in computer science, which
generally represents the Landau order term at <span class="math notranslate nohighlight">\(x=\infty\)</span>).  It means that all
x terms with power greater than or equal to <span class="math notranslate nohighlight">\(x^4\)</span> are omitted.  Order terms
can be created and manipulated outside of <code class="docutils literal notranslate"><span class="pre">series</span></code>.  They automatically
absorb higher order terms.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">+</span> <span class="n">x</span><span class="o">**</span><span class="mi">3</span> <span class="o">+</span> <span class="n">x</span><span class="o">**</span><span class="mi">6</span> <span class="o">+</span> <span class="n">O</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="mi">4</span><span class="p">)</span>
<span class="go">     3    ⎛ 4⎞</span>
<span class="go">x + x  + O⎝x ⎠</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span><span class="o">*</span><span class="n">O</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="go">O(x)</span>
</pre></div>
</div>
<p>If you do not want the order term, use the <code class="docutils literal notranslate"><span class="pre">removeO</span></code> method.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span><span class="o">.</span><span class="n">series</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">4</span><span class="p">)</span><span class="o">.</span><span class="n">removeO</span><span class="p">()</span>
<span class="go"> 2</span>
<span class="go">x</span>
<span class="go">── + x + 1</span>
<span class="go">2</span>
</pre></div>
</div>
<p>The <code class="docutils literal notranslate"><span class="pre">O</span></code> notation supports arbitrary limit points (other than 0):</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">exp</span><span class="p">(</span><span class="n">x</span> <span class="o">-</span> <span class="mi">6</span><span class="p">)</span><span class="o">.</span><span class="n">series</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">x0</span><span class="o">=</span><span class="mi">6</span><span class="p">)</span>
<span class="go">            2          3          4          5</span>
<span class="go">     (x - 6)    (x - 6)    (x - 6)    (x - 6)         ⎛       6       ⎞</span>
<span class="go">-5 + ──────── + ──────── + ──────── + ──────── + x + O⎝(x - 6) ; x → 6⎠</span>
<span class="go">        2          6          24        120</span>
</pre></div>
</div>
</section>
<section id="finite-differences">
<span id="calculus-finite-differences"></span><h2>Finite differences<a class="headerlink" href="#finite-differences" title="Permalink to this headline">¶</a></h2>
<p>So far we have looked at expressions with analytic derivatives
and primitive functions respectively. But what if we want to have an
expression to estimate a derivative of a curve for which we lack a
closed form representation, or for which we don’t know the functional
values for yet. One approach would be to use a finite difference
approach.</p>
<p>The simplest way the differentiate using finite differences is to use
the <code class="docutils literal notranslate"><span class="pre">differentiate_finite</span></code> function:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="p">,</span> <span class="n">g</span> <span class="o">=</span> <span class="n">symbols</span><span class="p">(</span><span class="s1">&#39;f g&#39;</span><span class="p">,</span> <span class="bp">cls</span><span class="o">=</span><span class="n">Function</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">differentiate_finite</span><span class="p">(</span><span class="n">f</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">*</span><span class="n">g</span><span class="p">(</span><span class="n">x</span><span class="p">))</span>
<span class="go">-f(x - 1/2)⋅g(x - 1/2) + f(x + 1/2)⋅g(x + 1/2)</span>
</pre></div>
</div>
<p>If you already have a <code class="docutils literal notranslate"><span class="pre">Derivative</span></code> instance, you can use the
<code class="docutils literal notranslate"><span class="pre">as_finite_difference</span></code> method to generate approximations of the
derivative to arbitrary order:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="n">Function</span><span class="p">(</span><span class="s1">&#39;f&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">dfdx</span> <span class="o">=</span> <span class="n">f</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">.</span><span class="n">diff</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">dfdx</span><span class="o">.</span><span class="n">as_finite_difference</span><span class="p">()</span>
<span class="go">-f(x - 1/2) + f(x + 1/2)</span>
</pre></div>
</div>
<p>here the first order derivative was approximated around x using a
minimum number of points (2 for 1st order derivative) evaluated
equidistantly using a step-size of 1. We can use arbitrary steps
(possibly containing symbolic expressions):</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="n">Function</span><span class="p">(</span><span class="s1">&#39;f&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d2fdx2</span> <span class="o">=</span> <span class="n">f</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">.</span><span class="n">diff</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">h</span> <span class="o">=</span> <span class="n">Symbol</span><span class="p">(</span><span class="s1">&#39;h&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d2fdx2</span><span class="o">.</span><span class="n">as_finite_difference</span><span class="p">([</span><span class="o">-</span><span class="mi">3</span><span class="o">*</span><span class="n">h</span><span class="p">,</span><span class="o">-</span><span class="n">h</span><span class="p">,</span><span class="mi">2</span><span class="o">*</span><span class="n">h</span><span class="p">])</span>
<span class="go">f(-3⋅h)   f(-h)   2⋅f(2⋅h)</span>
<span class="go">─────── - ───── + ────────</span>
<span class="go">     2        2        2</span>
<span class="go">  5⋅h      3⋅h     15⋅h</span>
</pre></div>
</div>
<p>If you are just interested in evaluating the weights, you can do so
manually:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">finite_diff_weights</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="p">[</span><span class="o">-</span><span class="mi">3</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">],</span> <span class="mi">0</span><span class="p">)[</span><span class="o">-</span><span class="mi">1</span><span class="p">][</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
<span class="go">[1/5, -1/3, 2/15]</span>
</pre></div>
</div>
<p>note that we only need the last element in the last sublist
returned from <code class="docutils literal notranslate"><span class="pre">finite_diff_weights</span></code>. The reason for this is that
the function also generates weights for lower derivatives and
using fewer points (see the documentation of <code class="docutils literal notranslate"><span class="pre">finite_diff_weights</span></code>
for more details).</p>
<p>If using <code class="docutils literal notranslate"><span class="pre">finite_diff_weights</span></code> directly looks complicated, and the
<code class="docutils literal notranslate"><span class="pre">as_finite_difference</span></code> method of <code class="docutils literal notranslate"><span class="pre">Derivative</span></code> instances
is not flexible enough, you can use <code class="docutils literal notranslate"><span class="pre">apply_finite_diff</span></code> which
takes <code class="docutils literal notranslate"><span class="pre">order</span></code>, <code class="docutils literal notranslate"><span class="pre">x_list</span></code>, <code class="docutils literal notranslate"><span class="pre">y_list</span></code> and <code class="docutils literal notranslate"><span class="pre">x0</span></code> as parameters:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">x_list</span> <span class="o">=</span> <span class="p">[</span><span class="o">-</span><span class="mi">3</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">y_list</span> <span class="o">=</span> <span class="n">symbols</span><span class="p">(</span><span class="s1">&#39;a b c&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">apply_finite_diff</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">x_list</span><span class="p">,</span> <span class="n">y_list</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
<span class="go">  3⋅a   b   2⋅c</span>
<span class="go">- ─── - ─ + ───</span>
<span class="go">   20   4    5</span>
</pre></div>
</div>
</section>
</section>


            <div class="clearer"></div>
          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="../index.html">
              <img class="logo" src="../_static/sympylogo.png" alt="Logo"/>
            </a></p>
  <h3><a href="../index.html">Table of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Calculus</a><ul>
<li><a class="reference internal" href="#derivatives">Derivatives</a></li>
<li><a class="reference internal" href="#integrals">Integrals</a></li>
<li><a class="reference internal" href="#limits">Limits</a></li>
<li><a class="reference internal" href="#series-expansion">Series Expansion</a></li>
<li><a class="reference internal" href="#finite-differences">Finite differences</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="simplification.html"
                        title="previous chapter">Simplification</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="solvers.html"
                        title="next chapter">Solvers</a></p>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="../_sources/tutorial/calculus.rst.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3 id="searchlabel">Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="https://docs.sympy.org/latest/search.html" method="get">
      <input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
      <input type="submit" value="Go" />
    </form>
    </div>
</div>
<script>$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="solvers.html" title="Solvers"
             >next</a> |</li>
        <li class="right" >
          <a href="simplification.html" title="Simplification"
             >previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="../index.html">SymPy 1.9 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="index.html" >SymPy Tutorial</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="#">Calculus</a></li> 
      </ul>
    </div>
    <div class="footer" role="contentinfo">
        &#169; Copyright 2021 SymPy Development Team.
      Last updated on Sep 30, 2021.
      Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.1.2.
    </div>
  </body>

<!-- Mirrored from docs.sympy.org/latest/tutorial/calculus.html by HTTrack Website Copier/3.x [XR&CO'2014], Sat, 15 Jan 2022 03:23:57 GMT -->
</html>